KISSY.add("pcm/mods/calender/calender", function(t, s, e, i, a) {
    function n(t) {
        var s = new Date;
        this.year = s.getFullYear(),
        this.month = s.getMonth() + 1,
        this.day = s.getDate(),
        this.root = t.calender,
        this.root && (this.setData(t.data),
        this.setHead(t.head),
        this.setPost(t.post))
    }
    var h = i.all
      , o = h(".movie-calender")
      , r = h(".movie-post")
      , d = new a(window.location.href)
      , l = (d.getQuery(),
    function() {
        return !!~location.hostname.indexOf("daily.taobao.net")
    }
    )
      , c = l() ? "//img.daily.taobaocdn.net/bao/uploaded/" : "//img.alicdn.com/tps/";
    return t.augment(n, {
        init: function() {
            var t = this;
            t.initHead(),
            t.showCurrentCalender(),
            o.animate({
                opacity: "1"
            }, .3),
            r.animate({
                opacity: "1"
            }, .3)
        },
        initHead: function(s) {
            this.monthPrev.removeClass("disable"),
            this.monthNext.removeClass("disable"),
            this.data.month[this.year + "-" + this.fix(this.month)] || (this.data.month[this.year + "-" + this.fix(this.month)] = 1),
            this.data.month[this.year + "-" + this.fix(this.month + 2)] && !this.data.month[this.year + "-" + this.fix(this.month) + 1] && (this.data.month[this.year + "-" + this.fix(this.month + 1)] = 1);
            var e = t.keys(this.data.month).sort();
            this.head.html(this.year + "\u5e74" + this.month + "\u6708"),
            0 == t.indexOf(this.year + "-" + this.fix(this.month), e) && this.monthPrev.addClass("disable"),
            2 == t.indexOf(this.year + "-" + this.month, e) && this.monthNext.addClass("disable")
        },
        ctrlSetting: function() {},
        setHead: function(t) {
            this.head = t.context,
            this.setHeadControl(t.prev, t.next)
        },
        setHeadControl: function(t, s) {
            var e = this;
            e.monthPrev = t,
            e.monthNext = s,
            t.on("click", function(t) {
                e.data.month[e.year + "-" + e.fix(e.month - 1)] ? e.resetTime(e.year, e.month - 1) : t.preventDefault()
            }),
            s.on("click", function(t) {
                e.data.month[e.year + "-" + e.fix(e.month + 1)] ? e.resetTime(e.year, e.month + 1) : (t.preventDefault(),
                h(t.target).addClass("disable"))
            })
        },
        setPost: function(t) {
            this.postImg = t.img,
            this.postMask = t.list,
            this.postInfo = t.info,
            this.dayInfo = t.dayInfo,
            this.postRank = t.rank,
            this.setPostCtrl(t.prev, t.next)
        },
        setPostCtrl: function(t, s) {
            var e = this;
            e.postPrev = t,
            e.postNext = s,
            t.on("click", function(t) {
                e.postIndex--,
                e.changePost()
            }),
            s.on("click", function(t) {
                e.postIndex++,
                e.changePost()
            })
        },
        setData: function(t) {
            for (var s = {}, e = {}, i = 0; i < t.length; i++) {
                var a = t[i].openDay.split(" ")[0]
                  , n = a.substring(0, 7);
                e[n] || (e[n] = 1),
                s[a] ? s[a].push(t[i]) : s[a] = [t[i]]
            }
            this.data = s,
            this.data.month = e
        },
        resetTime: function(t, s) {
            s > 12 && (t++,
            s = 1),
            0 >= s && (t--,
            s = 12),
            this.year = t,
            this.month = s,
            this.init()
        },
        showCurrentCalender: function() {
            this.month - 1 < 0 && (this.month = 0),
            this.week = new Date(this.year,this.month - 1,1).getDay(),
            this.count = this.currentDays(this.month),
            this.prevCount = this.currentDays(this.month - 1),
            this.renderList(this.buildData(this.week, this.count, this.prevCount))
        },
        buildData: function(t, s, e) {
            var i = new Date
              , a = (i.getDate(),
            i.getFullYear(),
            i.getMonth() + 1,
            0)
              , n = [];
            this.renderPost();
            for (var h = 0; t > h; h++)
                n.push({
                    day: ""
                });
            for (var h = 1; s >= h; h++)
                this.isActive(this.year, this.month, h) ? a ? n.push({
                    day: this.fix(h),
                    active: "active"
                }) : (a++,
                n.push({
                    day: this.fix(h),
                    current: "current"
                })) : n.push({
                    day: this.fix(h)
                });
            return n
        },
        fix: function(t) {
            return String(t).length < 2 ? "0" + String(t) : t
        },
        getValue: function(t, s, e) {
            return this.data[t + "-" + this.fix(s) + "-" + this.fix(e)] || !1
        },
        isActive: function(t, s, e) {
            return Boolean(this.getValue(t, s, e))
        },
        renderList: function(t) {
            var e = '{{#each data}}							{{@if current}}								<li class="current">{{day}}</li>							{{else}}								{{@if active}}									<li class="active">{{day}}</li>								{{else}}									<li>{{day}}</li>								{{/if}}							{{/if}}							{{/each}}';
            this.root.html(new s(e).render({
                data: t
            })),
            this.bindDayEvents()
        },
        bindDayEvents: function() {
            var t = this;
            t.root.delegate("click", "li", function(s) {
                var e = h(s.currentTarget);
                "LI" == s.currentTarget.nodeName && e.hasClass("active") && (h(".current", t.root).addClass("active"),
                e.addClass("current").siblings().removeClass("current"),
                t.renderPost(e.html()))
            })
        },
        renderPost: function(s) {
            var e = this;
            e.postIndex = 0;
            var i = []
              , a = 0
              , n = t.keys(e.data);
            n.sort(function(t, s) {
                return new Date(t.replace(/-/g, "/")).getTime() > new Date(s.replace(/-/g, "/")).getTime() ? 1 : -1
            });
            for (var h = 0; h < n.length; h++)
                if (new RegExp(e.year + "-" + e.fix(e.month)).test(n[h])) {
                    i = e.data[n[h]],
                    a = String(n[h]).split("-")[2];
                    break
                }
            e.postData = e.getValue(e.year, e.fix(e.month), e.fix(s)) || i,
            e.postDay = s || a,
            e.changePost()
        },
        changePost: function() {
            this.postPrev.removeClass("disable"),
            this.postNext.removeClass("disable");
            var t = "//img.alicdn.com/tps/i1/T1GtnmFBlXXXa8cM7S-124-176.jpg";
            if (this.postIndex <= 0 && (this.postIndex = 0,
            this.postPrev.addClass("disable")),
            this.postIndex >= this.postData.length - 1 && (this.postIndex = this.postData.length - 1,
            this.postNext.addClass("disable")),
            this.postData.length <= 1 && (this.postPrev.addClass("disable"),
            this.postNext.addClass("disable")),
            0 == this.postData.length)
                return h(".movie-name").html(""),
                this.postRank.parent().hide(),
                this.postImg.attr("src", ""),
                this.postMask.html("\u672c\u6708\u6682\u65e0\u65b0\u7247\u4e0a\u6620"),
                void this.dayInfo.html("\u672c\u6708\u6682\u65e0\u65b0\u7247\u4e0a\u6620");
            this.postData[this.postIndex] && "" != this.postData[this.postIndex].poster && (t = c + this.postData[this.postIndex].poster + "_220x10000Q95.jpg"),
            this.postImg.attr("src", t),
            this.postImg.parent("a").attr("href", "./showDetail.htm?showId=" + this.postData[this.postIndex].id + "&n_s=new");
            var e = "<li><em>\u5bfc\u6f14 :</em><span>{{director}}</span>					</li><li><em>\u4e3b\u6f14 :</em><span>{{leadingRole}}</span>					</li><li><em>\u7c7b\u578b :</em><span>{{type}}</span>					</li><li><em>\u5730\u533a :</em><span>{{country}}}</span>					</li><li><em>\u8bed\u8a00 :</em><span>{{language}}</span>					</li><li><em>\u7247\u957f :</em><span>{{duration}}\u5206\u949f</span>					</li>";
            this.postMask.html(new s(e).render(this.postData[this.postIndex])),
            this.postInfo.children(".movie-name").html(this.postData[this.postIndex] ? this.postData[this.postIndex].showName : ""),
            this.postRank.parent().show(),
            this.postRank.width(this.postData[this.postIndex] ? 10 * this.postData[this.postIndex].remark + "%" : "50%"),
            this.dayInfo.html(this.month + "\u6708" + this.fix(this.postDay) + "\u65e5 \u4e0a\u6620" + this.postData.length + "\u90e8"),
            0 == this.postData.length && (h(".movie-name").html(""),
            this.postRank.parent().hide(),
            this.postImg.attr("src", ""),
            this.postMask.html("\u672c\u6708\u6682\u65e0\u5f71\u7247\u4e0a\u6620"),
            this.dayInfo.html("\u6682\u65e0\u5f71\u7247"))
        },
        currentDays: function(t) {
            if (0 >= t)
                return 31;
            switch (t) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                return this.isLeapYear() ? 29 : 28
            }
        },
        isLeapYear: function() {
            var t = this.year;
            return t % 4 == 0 && t % 100 != 0 ? !0 : t / 400 == 0 ? !0 : !1
        }
    }),
    n
}, {
    requires: ["xtemplate", "event", "node", "uri", "pcm/mods/calender/calender.less.css"]
});
